package gui;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class guessnum {
    public  guessnum() {
       
            JFrame f = new JFrame();
			f.setVisible(true);
			f.setSize(400,400);
			// f.setExtendedState(JFrame.MAXIMIZED_BOTH);
			f.setLocationRelativeTo(null);
			f.setTitle("猜数字");
			
			Container con = f.getContentPane();
			con.setLayout(new GridLayout(3,1));

            JPanel pan1  = new JPanel();
            JLabel gues = new JLabel("猜数字游戏");
            pan1.add(gues);
            con.add(pan1);


            JPanel pan2 = new JPanel();
            JLabel  guess = new JLabel("请输入猜的数字");
            TextField guessnum  = new TextField(5);
            pan2.add(guess);
            pan2.add(guessnum);
            con.add(pan2);

            JPanel pan3 = new JPanel();
            JButton proceed = new JButton("猜大小");
            int number = (int) (Math.random() * 10) + 1;
            proceed.addActionListener(new ActionListener(){	
				@Override
				public void actionPerformed(ActionEvent arg0) {
			

                   while(!guessnum.getText().equals(String.valueOf(number))){
                    if (Integer.parseInt(guessnum.getText()) > number) {
                        // 执行猜大了的逻辑
                        JOptionPane.showMessageDialog(null, "猜大了！");
                        guessnum.setText("");
                    } else {
                        // 执行猜小了的逻辑
                        JOptionPane.showMessageDialog(null, "猜小了！");
                        guessnum.setText("");
                    }
                   }
                   JOptionPane.showMessageDialog(null, "猜对了！");
                   f.dispose();
				}
				
			});
            JButton exit = new JButton("退出");
            exit.addActionListener(new ActionListener(){
	
				@Override
				public void actionPerformed(ActionEvent arg0) {
					f.dispose();					
				}
				
			});
            pan3.add(proceed);
            pan3.add(exit);
            con.add(pan3);
      
    }
}
